<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Customer;
use App\Models\ChinaAreas;
use App\Models\ProSource;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Markdown;

/***
 *
 * @package App\Admin\Controllers
 * @description 客户列表
 * @author yundada
 * @date 2024/4/2
 * @version 1.0
 * @copyright
 */
class CustomerController extends AdminController
{
    /**
     *
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Customer(['user', 'prosource']), function (Grid $grid) {
            if (!Admin::user()->inRoles(['administrator', 'sale_admin', 'sales_executive'])) {
                $grid->model()->where('user_id', Admin::user()->id);
            }
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('phone');
            $grid->column('email');
            $grid->column('province_name', '省')->display(function ($value) {
                if (!empty($value)) {
                    return ChinaAreas::where('id', $value)->value('name');
                } else {
                    return '';
                }
            });
            $grid->column('city_name', '市')->display(function ($value) {
                if (!empty($value)) {
                    return ChinaAreas::where('id', $value)->value('name');
                } else {
                    return '';
                }
            });
            $grid->column('district_name', '区')->display(function ($value) {
                if (!empty($value)) {
                    return ChinaAreas::where('id', $value)->value('name');
                } else {
                    return '';
                }
            });
            $grid->column('address');
            $grid->column('sex')->using([1 => '男', 0 => '女']);
            $grid->column('identity')->display(function ($value) {
                if (!empty($value)) {
                    return \App\Models\Customer::$identity[$value];
                } else {
                    return '未录入';
                }
            });
            $grid->column('prosource.name', '项目来源');
            $grid->column('user.name', '销售人员');
            $grid->column('desc', '备注')->display('查看') // 设置按钮名称
            ->modal(function ($modal) {
                $modal->title('ID:' . $modal->row->id . '的内容');
                return new Markdown($modal->row->desc);
            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            if (Admin::user()->inRoles(['administrator', 'sale_admin', 'sales_executive'])) {
                $grid->export()->xlsx()->filename('客户数据');
            }
            $grid->filter(function (Grid\Filter $filter) {
                $filter->distpicker(['province_name', 'city_name', 'district_name'], '地域选择')->type('code');
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Customer(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('phone');
            $show->field('email');
            $show->field('address');
            $show->field('sex');
            $show->field('identity');
            $show->field('source');
            $show->field('user_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Customer(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->mobile('phone');
            $form->email('email');
//            $form->distpicker(['province_name', 'city_name', 'district_name'], '请选择区域');
            $form->distpicker([
                'province_name' => '省份',
                'city_name' => '市',
                'district_name' => '区'
            ], '地域选择')->type('code')->required();
            $form->text('address')->required();
            $form->radio('sex')->options([1 => '男', 0 => '女']);
            $form->select('identity')->options(\App\Models\Customer::$identity);
            $form->select('source')->options(ProSource::all()->pluck('name', 'id'));//来源
            if (Admin::user()->inRoles(['administrator', 'sale_admin'])) {
                $form->select('user_id', '销售人员')->options(\App\Models\AdminUser::all()->pluck('name', 'id'));
            } else {
                $form->hidden('user_id')->value(Admin::user()->id);

            }
            $form->textarea('desc', '备注')->help('可以在这里添加其他联系方式或者备注');
            $form->display('created_at');
            $form->display('updated_at');
        })->saving(function (Form $form) {
//            dd($form->input());

        });
    }
}
